package cc.sunni.user.service.impl;

import cc.sunni.api.utils.PageUtils;
import cc.sunni.pojo.user.AppUser;
import cc.sunni.user.service.AppUserMngService;
import cc.sunni.user.service.AppUserService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;

@Service
public class AppUserMngServiceImpl implements AppUserMngService {
    @Autowired
    public AppUserService appUserService;

    @Override
    public PageUtils queryAllUserList(String nickname, Integer status, Date startDate, Date endDate, Integer page, Integer pageSize) {
        IPage<AppUser> pages = appUserService.page(
                new Page<>(page, pageSize),
                // 拼接查询条件
                new LambdaQueryWrapper<AppUser>().orderByDesc(AppUser::getCreatedTime)
                        .like(StrUtil.isNotBlank(nickname), AppUser::getNickname, nickname)
                        .eq(status != null, AppUser::getActiveStatus, status)
                        .ge(startDate != null, AppUser::getCreatedTime, startDate)
                        .le(endDate != null, AppUser::getCreatedTime, endDate));
        return new PageUtils(pages);
    }

    @Transactional
    @Override
    public void freezeUserOrNot(String userId, Integer doStatus) {
        AppUser user = new AppUser();
        user.setId(userId);
        user.setActiveStatus(doStatus);
        appUserService.updateById(user);
    }
}
